﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Gollie.Controls
{
	public partial class YearPickerControl : BaseUserControl
	{
		int yearHigh = DateTime.Today.Year;
		int numberOfYears = 100;

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.SetYears();
		}

		public int YearHigh
		{
			get { return this.yearHigh; }
			set { this.yearHigh = value; this.SetYears(); }
		}

		public int NumberOfYears
		{
			get { return this.numberOfYears; }
			set { this.numberOfYears = value; this.SetYears(); }
		}

		public int SelectedYear
		{
			get
			{
				string selectedValue = this.DropDownList1.SelectedValue;

				return Convert.ToInt32(selectedValue);
			}
			set
			{
				try
				{
					this.DropDownList1.SelectedValue = value.ToString();
				}
				catch (ArgumentOutOfRangeException)
				{
				}
			}
		}

		private void SetYears()
		{
			this.DropDownList1.Items.Clear();

			int year = this.yearHigh;

			for (int i = 0; i < this.numberOfYears; i++)
			{
				this.DropDownList1.Items.Add(new ListItem(year.ToString()));

				year--;
			}

			this.DropDownList1.SelectedIndex = 0;
		}
	}
}